package com.gooluke.mds.common.annotation;

import lombok.Getter;
import org.springframework.transaction.TransactionDefinition;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 指定同时操作多数据源
 */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MultiDataSource {
    /**
     * 数据源列表
     */
    String[] names();

    /**
     * 事务隔离级别 默认 READ_COMMITTED
     */
    IsolationLevel isolationLevel() default IsolationLevel.READ_COMMITTED;

    /**
     * 事务传播行为 默认  PROPAGATION_REQUIRES_NEW
     */
    PropagationBehavior propagation() default PropagationBehavior.PROPAGATION_REQUIRES_NEW;




    /**
     * 事务隔离级别
     *
     * @see TransactionDefinition
     */
    @Getter
    enum IsolationLevel {
        READ_COMMITTED(2),
        READ_UNCOMMITTED(4),
        REPEATABLE_READ(6),
        SERIALIZABLE(8),
        ;

        private final int level;

        IsolationLevel(int level) {
            this.level = level;
        }
    }

    /**
     * 事务传播行为
     *
     * @see TransactionDefinition
     */
    @Getter
    enum PropagationBehavior {
        PROPAGATION_REQUIRED(0),
        PROPAGATION_SUPPORTS(1),
        PROPAGATION_MANDATORY(2),
        PROPAGATION_REQUIRES_NEW(3),
        PROPAGATION_NOT_SUPPORTED(4),
        PROPAGATION_NEVER(5),
        PROPAGATION_NESTED(6),
        ;

        private final int behavior;

        PropagationBehavior(int behavior) {
            this.behavior = behavior;
        }
    }
}